package cn.wlinker.video.monitor.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.wlinker.video.monitor.domain.VideoBaseEntity;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 监控设备缓存
 *
 * @author gxsjx
 * @version 1.0
 * @date 2022/7/27
 */
public class MonitorDeviceCacheUtils {

    public static String getMonitorDeviceKey(VideoBaseEntity videoBaseEntity) {
        return getMonitorDeviceKey(videoBaseEntity.getMonitorType(), videoBaseEntity.getAccessPlatform(), videoBaseEntity.getDeviceCode(), videoBaseEntity.getChannelNo());
    }

    public static String getMonitorDeviceKey(String monitorType, String accessPlatform, String deviceCode, Integer channelNo) {
        if (ObjectUtil.isEmpty(channelNo)) {
            return StrUtil.format("{}-{}-{}", monitorType, accessPlatform, deviceCode);
        } else {
            return StrUtil.format("{}-{}-{}-{}", monitorType, accessPlatform, deviceCode, channelNo);
        }
    }

    private static final Map<String, VideoBaseEntity> MONITOR_DEVICE_VO_MAP = new ConcurrentHashMap<>();

    public static VideoBaseEntity get(String monitorDeviceKey) {
        return MONITOR_DEVICE_VO_MAP.get(monitorDeviceKey);
    }

    public static void put(String monitorDeviceKey, VideoBaseEntity monitorDeviceVO) {
        MONITOR_DEVICE_VO_MAP.put(monitorDeviceKey, monitorDeviceVO);
    }

    public static void put(VideoBaseEntity videoBaseEntity) {
        String monitorDeviceKey = getMonitorDeviceKey(videoBaseEntity);
        Integer channelNo = videoBaseEntity.getChannelNo();
        if(ObjectUtil.isEmpty(channelNo)){
            videoBaseEntity.setChannelNo(1);
        }
        MONITOR_DEVICE_VO_MAP.put(monitorDeviceKey, videoBaseEntity);
    }

    public static void put(List<VideoBaseEntity> videoBaseEntityList) {
        videoBaseEntityList.forEach(MonitorDeviceCacheUtils::put);
    }
}
